﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using GRTVPluginConsole.Utils;
using GRTVPluginConsole.Listener;
using System.Net;
using GRTVPluginConsole.Webserver;
using GRTVPluginConsole.Entity;
using GRTVPluginConsole.UI;

namespace GRTVPluginConsole
{
    public static class Program
    {
        public static Configuration configuration = new Configuration();
        public static MainUI form1;

        public static event DriverSelected DriverSelectedEvent;
        public delegate void DriverSelected(object sender, DriverRank driverRank);

        public static DriverRankUI driverRankUI;
        public static EventUI eventReplayUI;
        public static WeatherUI widgetUI;
        public static AutoCameraUI autoCameraUI;
        public static LiveTrackUI liveTrackUI;
        /// <summary>
        /// Point d'entrée principal de l'application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            string s = System.IO.Directory.GetCurrentDirectory();
            configuration.iniFile = new IniFile(s + "\\GRTVPluginConsole.ini");
            configuration.iniTrackFile = new IniFile(s + "\\GRTVPluginConsole-Track.ini");
            configuration.corePath = configuration.iniFile.IniReadValue("RFACTOR2", "CorePath");
            configuration.gdbExtractPath = configuration.iniFile.IniReadValue("RFACTOR2", "GDBExtractPath");
            //configuration.corePath = configuration.iniFile.IniReadValue("RFACTOR2", "CorePath");
            //configuration.gdbExtractPath = configuration.iniFile.IniReadValue("RFACTOR2", "GDBExtractPath");
            //configuration.defaultWUAPIKey = configuration.iniFile.IniReadValue("SERVER", "DefaultWeatherUndergroundAPIKey");

            string str = configuration.iniFile.IniReadValue("Console", "Port");
            configuration.serverPort = Int32.Parse(str);

            str = configuration.iniFile.IniReadValue("Plugin", "Address");
            configuration.pluginAddress = IPAddress.Parse(str);
            str = configuration.iniFile.IniReadValue("Plugin", "Port");
            configuration.pluginPort = Int32.Parse(str);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            PluginListenerThread.Start();

            form1 = new MainUI();
            driverRankUI = new DriverRankUI();
            eventReplayUI = new EventUI();
            widgetUI = new WeatherUI();
            autoCameraUI = new AutoCameraUI();
            liveTrackUI = new LiveTrackUI();

            form1.setUI(0, driverRankUI);
            form1.setUI(1, eventReplayUI);
            form1.setUI(2, widgetUI);
            form1.setUI(3, autoCameraUI);
            form1.setUI(4, liveTrackUI);
            // TODO remove
            //DriverRank dr = new DriverRank();
            //dr.slotId = 23;
            //dr.place = 1;
            //dr.name = "Amphi";
            //dr.finishStatus = 0;
            //dr.isInPit = false;
            //form1.driverRanks.Add(dr);
            //dr = new DriverRank();
            //dr.slotId = 13;
            //dr.place = 2;
            //dr.name = "Mad_King";
            //dr.finishStatus = 0;
            //dr.isInPit = true;
            //form1.driverRanks.Add(dr);

            Application.Run(form1);
            

        }



        public static string[] CameraList
        {
            get
            {
                string[] list = new string[1006];
                list[0] = "Current";
                list[1] = "TV cockpit";
                list[2] = "Cockpit";
                list[3] = "Nosecam";
                list[4] = "Swingman";
                list[5] = "Trackside (nearest)";

                string s;
                for (int i = 0; i < 1000; i++)
                {
                    s = "onboard" + i;
                    list[6 + i] = s;
                }
                return list;
            }
        }

        public static string[] WidgetList
        {
            get
            {
                string[] list = new string[15];
                list[0] = "DriverPanel";
                list[1] = "DriverTiming";
                list[2] = "ScrollingBanner";
                list[3] = "WeatherPanel";
                list[4] = "SessionStatus";
                list[5] = "UIInfo";
                list[6] = "Overtake";
                list[7] = "Compare3Driver";
                list[8] = "Tower";
                list[9] = "SpeedTrap";
                list[10] = "WeatherFull";
                list[11] = "CuttingLineGap";
                list[12] = "FastestLap";
                list[13] = "UIBackground";
                list[14] = "Winner";
                return list;
            }
        }

        public static List<DriverRank> DriverList
        {
            get
            {
                return new List<DriverRank>(driverRankUI.driverRanks);
            }
        }

        public static string[] EventTypeList
        {
            get
            {
                string[] list = new string[5];
                list[0] = "All";
                list[1] = "Overtake";
                list[2] = "Finish status";
                list[3] = "Off track";
                list[4] = "Spin Around";
                return list;
            }
        }

        public static List<Event> GetEventList(int eventTypeIndex)
        {
            int filter = 65535;
            if (eventTypeIndex > 0)
            {
                filter = (int)Math.Pow(2, eventTypeIndex - 1);
            }
            List<Event> iter = new List<Event>(eventReplayUI.eventListModel);
            List<Event> result = new List<Event>();
            for (int i = 0; i < iter.Count; i++)
            {
                if (eventReplayUI.isEventOfType(iter[i], filter))
                {
                    result.Add(iter[i]);
                }
            }
            return result;
        }

        public static void ViewDriver(int viewedSlotId, int cameraType)
        {
            driverRankUI.viewDriver(viewedSlotId, cameraType);
        }


        public static void sendWidgetVisibility(byte widgetIndex, byte vis)
        {
            widgetUI.sendWidgetVisibility(widgetIndex, vis);
        }

        public static void sendPluginCommand(byte cmdType, byte cmdValue)
        {
            widgetUI.sendPluginCommand(cmdType, cmdValue);
        }

        public static void sendGoLive()
        {
            eventReplayUI.sendGoLive();
        }

        public static void sendReplayEvent(string uuid, int seconds, int cameraIndex)
        {
            eventReplayUI.sendReplayEvent(uuid, seconds, cameraIndex);
        }

        public static void SetSelectedDriver(object sender, DriverRank driver) {
            if (Program.DriverSelectedEvent != null)
            {
                Program.DriverSelectedEvent(sender, driver);
            }
        }
    }
}
